// pages/scanningCode/index.js
import {
  config
} from "../../config"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roundIndex:0,
    text:'',
    isShowSuccess:false,
    timerTask:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      roundIndex:options.round
    })
    const openId = wx.getStorageSync('openid')
    const index = parseInt(this.data.roundIndex)+1
    const testData = `openid=${openId}&g${index}=1`
    this.setData({
      text:`https://api.shanghaichujie.com/api/qrcode/generate?text=${escape(testData)}`
    })
    let self = this;
    self.timer = setInterval(() => {
      this.getUserData()
    },1500);
  },
  getUserData() {
    console.log(this.timer)
    const openId = wx.getStorageSync('openid')
    const index = parseInt(this.data.roundIndex)+1
    wx.request({
      url: `${config.api_blink_url}/redbull/user?openid=${openId}`,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        const gIndex = `g${index}`
       if(res.data[gIndex]===1){
        this.setData({
          isShowSuccess:true
        })
        wx.redirectTo({
          url: '../checkpoint/index'
        })
        clearInterval(this.timer);
       }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})